#! python
# -*- coding: utf-8 -*-

from time import time
import noise_generator, treasure, portal_generator, health_point_generator
from pysvg import structure, shape

from objects_on_map import get_items_on_map

DEBUG = False
PIXEL = 5


def get_svg(type_of_map=None):
    count_line = 0

    pixels = get_items_on_map()
    s = structure.Svg(width=3800, height=2500)

    if type_of_map is 0:
        start_time_map = time()

        altitude_noise = noise_generator.generate_altitude_noise()
        print "Altitude noise generated"

        ferrum_chrome_noise = noise_generator.generate_chemical_noise(1)
        print "Chrome vs Ferrum noise is generated"

        silicone_noise = noise_generator.generate_chemical_noise(2)
        print "Silicone noise is generated"

        pegmatite_noise = noise_generator.generate_chemical_noise(3)
        print "Pegmatite noise is generated"

        treasured_noise = treasure.make_treasure(altitude_noise, ferrum_chrome_noise, silicone_noise, pegmatite_noise)
        print "Treasure generating completed"

        portal_noise = portal_generator.generate_portals_on_map(treasured_noise)
        print "Portals generated"

        start_time = time()
        health_noise = health_point_generator.generate_health_points_on_map(portal_noise)
        end_time = time() - start_time
        print "Health points generated %f" % end_time

        noise = health_noise

        end_time_map = time() - start_time_map
        print "=== completed ===="
        print "%f sec" % end_time_map

        if DEBUG:
            print noise

        for list_noise in noise:
            count_chunk = 0

            for pixel in list_noise:

                color = pixels[pixel]

                if color is None:
                    color = '#000000'

                i = shape.Rect(x=count_chunk * PIXEL, y=count_line * PIXEL, width=PIXEL, height=PIXEL, fill=color)
                s.addElement(i)

                count_chunk += 1
            count_line += 1

    elif type_of_map is 10:
        altitude_noise = noise_generator.generate_altitude_noise()
        print "Altitude noise generated"

        ferrum_chrome_noise = noise_generator.generate_chemical_noise(1)
        print "Chrome vs Ferrum map is generated"

        silicone_noise = noise_generator.generate_chemical_noise(2)
        print "Silicone noise is generated"

        pegmatite_noise = noise_generator.generate_chemical_noise(3)
        print "Pegmatite noise is generated"

        noise = treasure.make_treasure(altitude_noise, ferrum_chrome_noise, silicone_noise, pegmatite_noise)
        print "Treasure generating completed"

        if DEBUG:
            print noise

            #for list_noise in noise:
                #print ''.join('#' if el else '-' for el in list_noise)
                #print ''.join(str(el) if el else '-' for el in list_noise)

        pixels[0] = '#5d5d5d'
        for _ in xrange(1, 9):
            pixels[_] = '#0e0d0d'

        for list_noise in noise:
            count_chunk = 0

            for pixel in list_noise:

                color = pixels[pixel]

                if color is None:
                    color = '#000000'

                i = shape.Rect(x=count_chunk * PIXEL, y=count_line * PIXEL, width=PIXEL, height=PIXEL, fill=color)
                s.addElement(i)

                count_chunk += 1
            count_line += 1

    # Chrome vs Ferrum
    elif type_of_map is 1:
        noise = noise_generator.generate_chemical_noise(1)
        print "Chrome vs Ferrum map is generated"

        for list_noise in noise:
            count_chunk = 0

            for pixel in list_noise:
                if pixel == 0:  # Chrome
                    color = '#00eaff'
                elif pixel == 1:  # Iron
                    color = '#AAAAAA'
                else:
                    color = '#111111'

                i = shape.Rect(x=count_chunk * PIXEL, y=count_line * PIXEL, width=PIXEL, height=PIXEL, fill=color)
                s.addElement(i)

                count_chunk += 1
            count_line += 1

    # Silicone map
    elif type_of_map is 2:
        noise = noise_generator.generate_chemical_noise(2)
        print "Silicone vs Titan map is generated"

        for list_noise in noise:
            count_chunk = 0

            for pixel in list_noise:
                if pixel == 0: # None element - free space
                    color = '#222222'
                elif pixel == 1: # Silicone low density
                    color = '#ff18a5'
                elif pixel == 2: # Titan area
                    color = '#ff9900'
                else:
                    color = '#000000'

                i = shape.Rect(x=count_chunk * PIXEL, y=count_line * PIXEL, width=PIXEL, height=PIXEL, fill=color)
                s.addElement(i)

                count_chunk += 1
            count_line += 1

    # Pegmatite map
    elif type_of_map is 3:
        noise = noise_generator.generate_chemical_noise(3)
        print "Pegmatite map is generated"

        for list_noise in noise:
            count_chunk = 0

            for pixel in list_noise:
                if pixel == 0:  # None element - free space
                    color = '#222222'
                elif pixel == 1:  # Pegmatite low density
                    color = '#39435a'
                elif pixel == 2:  # Pegmatite high area
                    color = '#0f2c6e'
                else:
                    color = '#000000'

                i = shape.Rect(x=count_chunk * PIXEL, y=count_line * PIXEL, width=PIXEL, height=PIXEL, fill=color)
                s.addElement(i)

                count_chunk += 1
            count_line += 1

    # Resort permutation
    noise_generator.refresh_map_generator()
    return s.getXML()
